const { BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const setGlobalShoutCut = require(path.join(
  __dirname,
  "../shortcut/shortcut.js"
));
const store = require(path.join(__dirname, "../../store/store"));
module.exports.createToolsWin = (parent) => {
  win = new BrowserWindow({
    width: 600,
    height: 600,
    parent: parent,
    show: false,
    modal: true,
    frame: false,
    resizable: false,
    maximizable: false,
    webPreferences: {
      preload: path.join(__dirname, "../preload.js"),
    },
  });
  win.loadFile(path.join(__dirname, "../../page/tools/index.html"));
  return win;
};

ipcMain.handle("sendKey", (event, mes) => {
  return setGlobalShoutCut.setShoutCut(event, mes.type, mes);
});
ipcMain.handle("isOpenVoice", (event, mes) => {
  if (mes && typeof mes.isOpenVoice === "boolean")
    store.set("isOpenVoice", mes.isOpenVoice);
  if (mes && mes.username) store.set("username", mes.username);
  return {
    isOpenVoice: store.has("isOpenVoice") ? store.get("isOpenVoice") : true,
    username: store.has("username") ? store.get("username") : true,
  };
});
ipcMain.handle("getFastKey", () => {
  console.log(store.get("voice"), store.get("mic"));
  return {
    voice: store.has("voice") ? store.get("voice") : null,
    mic: store.has("mic") ? store.get("mic") : null,
    username: store.has("username") ? store.get("username") : null,
  };
});
